package rs.etf.mips.crossroad.logic;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class OutOfBounds implements ITrafficSpace {
	
	private static OutOfBounds instance = null;
	private List<Vehicle> vehicles;
	
	public static OutOfBounds getInstance(){
		if (instance == null) {
			instance = new OutOfBounds();
		}
		return instance;
	}

	private OutOfBounds() {
		super();
		this.vehicles = new ArrayList<>();
	}

	@Override
	public Iterator<Vehicle> iterator() {
		return this.vehicles.iterator();
	}

	@Override
	public void update() {
		// just ignore
	}

	@Override
	public boolean move(Vehicle vehicle) {
		return false;
	}

	@Override
	public boolean canMove(Vehicle vehicle) {
		return false;
	}

	@Override
	public boolean isFree(double x, double y) {
		return true;
	}

	@Override
	public boolean addVehicle(Vehicle vehicle) {
		boolean added = this.vehicles.add(vehicle);
		if (added) vehicle.setSpace(this);
		return added;
	}

	@Override
	public boolean addVehicle(Vehicle vehicle, ITrafficSpace neighbor) {
		return this.addVehicle(vehicle);
	}

	/**
	 * call when data about the vehicles that moved out of the crossroad are no longer needed.
	 */
	public void clear(){
		this.vehicles = new ArrayList<>();
	}

	@Override
	public String toString() {
		StringBuffer line = new StringBuffer("OutOfBounds []");
		Iterator<Vehicle> iter = this.vehicles.iterator();

		while (iter.hasNext()) {
			line.append("\n\t" + iter.next().toString());
		}

		return line.toString();
	}
	
	
}
